package com.haiyou.data.common.accessor.redis.redisson.bucket;

import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

import com.google.common.collect.Maps;
import com.haiyou.common.entity.Entity;
import com.haiyou.data.common.cache.redis.RKeyModuleCaster;

/**
 * 
 * 
 * @Description: 单个key 的对象桶处理器
 * @author xingyuan
 * @date 2022年3月9日 下午6:48:08
 */
public class KeyBucketAccessor<E extends Entity<String>> extends RedissonBucketAccessor<E>{

	@Override
	public E remove(Object key) {
		key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.remove(key);
	}
	
	public E remove() {
		String key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.remove(key);
	}

	@Override
	public void removeAll(Iterable<? extends String> keys) {
		remove(null);
	}

	@Override
	public E compute(String key, BiFunction<? super String, ? super E, ? extends E> remappingFunction) {
		key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.compute(key, remappingFunction);
	}

	public E compute(BiFunction<? super String, ? super E, ? extends E> remappingFunction) {
		return compute(null, remappingFunction);
	}
	
	@Override
	public E get(Object key) {
		key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.get(key);
	}

	public E get() {
		String key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.get(key);
	}
	
	
	@Override
	public E get(String key, Function<? super String, ? extends E> mappingFunction) {
		key = RKeyModuleCaster.castKeyTo(valueClass);
		return super.get(key, mappingFunction);
	}
	
	public E get(Function<? super String, ? extends E> mappingFunction) {
		String key = RKeyModuleCaster.castKeyTo(valueClass);
		return get(key, mappingFunction);
	}

	@Override
	public Map<String, E> getAll(Iterable<? extends String> keys) {
		String key = RKeyModuleCaster.castKeyTo(valueClass);
		E e = super.get(key);
		Map<String, E> maps = Maps.newHashMap();
		maps.put(key, e);
		return maps;
	}

	@Override
	public Class<?> getInitClass() {
		return this.getClass();
	}
	
}
